package com.power.sifa.std.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.std.model.Std_incourt;
import com.power.sifa.std.model.pageInfo.Std_incourtPageInfo;
import com.power.sifa.std.api.IStd_incourtFeign;
import com.power.sifa.std.service.Std_incourtService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   审理信息出庭类型控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Std_incourtController",tags={"审理信息出庭类型"})
public class Std_incourtController implements IStd_incourtFeign{

    @Resource
    private Std_incourtService std_incourtService;


    @Override
    @ApiOperation("查询审理信息出庭类型分页列表")
    @SaCheckPermission("std_incourt_list")
    public ResponseDTO list(Std_incourtPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Std_incourt> page = this.std_incourtService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Std_incourt std_incourt : page) {
                std_incourt.geneText();
                list.add(std_incourt);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看审理信息出庭类型")
    @SaCheckPermission("std_incourt_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Std_incourt std_incourt  = std_incourtService.getById(id);
        if (std_incourt != null) {
            std_incourt.geneText();
        }
        return ResponseDTO.succData(std_incourt);
    }

    @Override
    @ApiOperation("删除审理信息出庭类型")
    @SaCheckPermission("std_incourt_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        std_incourtService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改审理信息出庭类型")
    @SaCheckPermission("std_incourt_save")
    public ResponseDTO save(Std_incourt std_incourt) {
        Assert.notNull(std_incourt, "std_incourt不能为空");
        std_incourtService.saveOrUpdate(std_incourt);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除审理信息出庭类型")
    @SaCheckPermission("std_incourt_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        std_incourtService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("std_incourt_list")
    public void output(HttpServletResponse response, Std_incourtPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "审理信息出庭类型";
        String[] heads = {"序号","类别名称","父级id"};
        String[] fields = {"id","name","parentid"};
        Page<Std_incourt> page = this.std_incourtService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "审理信息出庭类型";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","类别名称","父级id"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","name","parentid"};
        List<Std_incourt> list = (List<Std_incourt>)ExcelUtil.Import(fields, file, Std_incourt.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.std_incourtService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO tree() {
        try {
            return ResponseDTO.succData(this.std_incourtService.tree());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/std_incourt/list 查询出庭类型树失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "查询出庭类型树失败");
        }
    }

}
